package org.opens.protobuf.test;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.opens.protobuf.MessageProto;

import java.util.List;

/**
 * 功能描述
 *
 * @author: zhangyuming
 */
public class CombinedProtobufDecoder extends MessageToMessageDecoder<ByteBuf> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        // 解析 MessageWrapper 消息
        MessageProto.MessageWrapper wrapper = MessageProto.MessageWrapper.parseFrom(msg.nioBuffer());

        // 根据消息类型处理消息
        switch (wrapper.getType()) {
            case MY_MESSAGE:
                out.add(wrapper.getMyMessage());
                break;
            case NETWORK_ACCESS:
                out.add(wrapper.getNetworkAccess());
                break;
            case WAV_MESSAGE:
                out.add(wrapper.getWavMessage());
                break;
            default:
                System.err.println("Unknown message type: " + wrapper.getType());
                break;
        }
    }

}
